package com.mall.manager.userpart.controller;

import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONArray;

import org.apache.commons.collections.MapUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.util.WebUtils;

import com.mall.manager.part.service.ManagePartService;
import com.mall.manager.user.service.ManageUserService;
import com.mall.manager.userpart.service.ManageUserPartService;
import com.mall.untils.Page;

/**用户角色控制
 * @author zhaopeisen
 *
 */
@Controller
public class ManageUserPartController {
	
	 Logger logger = LoggerFactory.getLogger(ManageUserPartController.class);
	 @Resource 
	 ManageUserPartService manageuserpartService;
	 @Resource
	 ManageUserService  manageruserService;
	 @Resource
	 ManagePartService  managerpartService;
	 
	 /**获取用户角色列表
	 * @param request
	 * @param response
	 * @param modelMap
	 * @return
	 */
	@RequestMapping(value = "/manager_user_part_list")
	    public String manager_user_part_list(HttpServletRequest request, HttpServletResponse response, ModelMap modelMap) {
			Map paraMap = WebUtils.getParametersStartingWith(request, "");
			List users=manageruserService.manager_user_lists(request);
	    	List parts=manageuserpartService.manager_listbypart(request);
	    	int user_id = MapUtils.getIntValue(paraMap, "user_id");
	    	modelMap.addAttribute("user_id", user_id);
	     	if(null!=users){
		     	modelMap.addAttribute("manager_user_list", (List<Map<String,Object>>)users);
	     	}
	     	if(null!=parts){
	     		modelMap.addAttribute("manager_part_list", (List<Map<String,Object>>)parts);
	     	}
	    	return "userpart/manager_user_part";
	    }

	/**
     * 按照用户显示列表
     * @param request
     * @param response
     * @param modelMap
     * @return
     */
    @RequestMapping(value = "/manager_listbyUser")
    public String manager_listbyUser(HttpServletRequest request, HttpServletResponse response, ModelMap modelMap) {
    	Page page=manageuserpartService.manager_listbyUser(request);
     	if(null!=page){
	     	modelMap.addAttribute("manager_listbyUser", (List<Map<String,Object>>)page.getResult());
	     	int totalpagecount = page.getTotalPageCount();
	     	modelMap.addAttribute("totalpagecount", totalpagecount);
	     	int currentpageno = page.getCurrentPageNo();
	     	modelMap.addAttribute("currentpageno", currentpageno);
	     	int previouspage = page.getCurrentPageNo() <= 1 ? 1 : page
					.getCurrentPageNo() - 1;
	     	modelMap.addAttribute("previouspage", previouspage);
	     	int nextpage = page.getCurrentPageNo() >= page.getTotalPageCount() ? page
					.getTotalPageCount() : page.getCurrentPageNo() + 1;
			modelMap.addAttribute("nextpage", nextpage); 
			List<Integer> showNumList = page.getShowNumList();
			modelMap.addAttribute("pagenumlist", showNumList);
     	}
    	return "manager_listbyUser";
    }
    
    /**
     * 按照角色显示列表
     * @param request
     * @param response
     * @param modelMap
     * @return
     */
    @ResponseBody 
    @RequestMapping(value = "/manager_listbyPart")
    public String manager_listbyPart(HttpServletRequest request, HttpServletResponse response, ModelMap modelMap) {
    	List<Map<String, Object>> manager_part_lists = managerpartService.manager_part_lists(request);
     	if(null!=manager_part_lists){
     		JSONArray jsonarray = JSONArray.fromObject(manager_part_lists);  
            return jsonarray.toString(); 
     	}
		return null;
    }
    
    /**
     * 添加用户的角色
     * @param request
     * @param response
     * @param modelMap
     * @return
     */
    @RequestMapping(value = "/manager_userpart_add")
    public String manager_userpart_add(HttpServletRequest request, HttpServletResponse response, ModelMap modelMap) {
    	return "manager_userpart_add";
    }
    
    /**
     * 保存用户的角色
     * @param request
     * @param response
     * @param modelMap
     * @return
     */
    @RequestMapping(value = "/manager_userpart_save")    
    @ResponseBody
    public String manager_userpart_save(HttpServletRequest request, HttpServletResponse response, ModelMap modelMap) {
    	Map<String, Object> paraMap = WebUtils.getParametersStartingWith(request, "");
    	int update=manageuserpartService.manager_userpart_save(paraMap);
    	return String.valueOf(update);
    }
    
    /**
     * 修改用户的角色没想好
     * @param request
     * @param response
     * @param modelMap
     * @return
     */
//    @RequestMapping(value = "/manager_userpart_edit")    
//    public String manager_power_edit(HttpServletRequest request, HttpServletResponse response, ModelMap modelMap) {
//    	Page parts = manageuserpartService.manager_listbypart(request);
//    	modelMap.addAttribute("parts", parts);
//    	return "manager_power_edit";
//    }
    
    /**
     * 更新用户角色
     * @param request
     * @param response
     * @param modelMap
     * @return
     */
    @RequestMapping(value = "/manager_userpart_update")    
    @ResponseBody
    public String manager_power_update(HttpServletRequest request, HttpServletResponse response, ModelMap modelMap) {
    	Map<String, Object> paraMap = WebUtils.getParametersStartingWith(request, "");
    	int update=manageuserpartService.manager_power_update(paraMap);
    
    	if(update==1){
    		return "success";
    	}else{
    		return "failure";
    	}
		
    }
    
    /**
     * 删除用户的角色
     * @param request
     * @param response
     * @param modelMap
     * @return
     */
    @RequestMapping(value = "/manager_userpart_delete")    
    @ResponseBody
    public String manager_userpart_delete(HttpServletRequest request, HttpServletResponse response, ModelMap modelMap) {
    	Map<String, Object> paraMap = WebUtils.getParametersStartingWith(request, "");
    	int update=manageuserpartService.manager_userpart_delete(paraMap);
    	return String.valueOf(update);
    }
}
